package cn.lili.controller;

import cn.lili.cache.impl.RedisCache;
import cn.lili.modules.constant.CommonConstant;
import cn.lili.modules.order.order.client.OrderClient;
import cn.lili.modules.order.order.client.RankingDistributionClient;
import cn.lili.modules.order.order.entity.dto.RankingFySetParentChildCountDTO;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/consumer/rankingDistribution")
@Api(tags = "异常店主排名消息API")
@RequiredArgsConstructor
public class RankingDistributionController {

    private final RankingDistributionClient rankingDistributionClient;
    private final OrderClient orderClient;
    @Resource
    private RedisCache redisCache;
    @GetMapping("/fySetParentSyncMallRankingDistribution")
    @ApiOperation("飞语绑定上级,检测是否是商城店主,并对商城的店主执行下级+1操作")
    public void fySetParentSyncMallRankingDistribution(String str) {
        RankingFySetParentChildCountDTO dto = new RankingFySetParentChildCountDTO();
        JSONObject jsonObject = JSONObject.parseObject(str);
        dto.setNum(jsonObject.getInteger("num"));
        dto.setParentOpenId(jsonObject.getString("parentOpenId"));
        rankingDistributionClient.fySetParentSyncMallRankingDistribution(dto);
    }


    @GetMapping("/executeCityDistributionComiss")
    @ApiOperation("执行定时任务-发放城主收益")
    public void  executeCityDistributionComissLastMonthOrder(){
        orderClient.executeCityDistributionComissLastMonthOrder();
    }

    @GetMapping("/executeCityDistribution")
    @ApiOperation("执行定时任务-店主升级城主")
    public void  executeCityDistributionLastMonthOrder(){
        orderClient.executeCityDistributionLastMonthOrder();
    }

    @GetMapping("/executeUnionComiss")
    @ApiOperation("执行定时任务-发放工会收益")
    public void  executeUnionComiss(){
        //orderClient.executeUnionGreenScoreLastMonthOrder();
    }


    @PostMapping("/putCityDisUpgradeOrComiss")
    @ApiOperation("升级城主或发放收益redis配置, type: 1,升级城主 2,城主发放收益 ,onlyQuery: 0,只查询不修改 1,修改并返回 , upgradeDate升级城主日期, taskTime 任务时间"
  +  "{'type':1,\n'onlyQuery':1,\n'upgradeDate': 15,\n'taskTime': 0\n}"
    )
    public Object putCityDisUpgradeOrComiss(@RequestBody JSONObject jsonObject) {
        Map<String, Object> resultMap = new HashMap<>(4);
        Integer type = jsonObject.getInteger("type");
        Integer onlyQuery = jsonObject.getInteger("onlyQuery");
        Integer upgradeDate = jsonObject.getInteger("upgradeDate");
        Integer taskTime = jsonObject.getInteger("taskTime");
        if (type == 1) {
            String upgrade_citydis_task_date = redisCache.getString(CommonConstant.UPGRADE_CITYDIS_TASK_DATE);
            String upgrade_citydis_task_time = redisCache.getString(CommonConstant.UPGRADE_CITYDIS_TASK_TIME);
            if (onlyQuery == 1) {
                redisCache.put(CommonConstant.UPGRADE_CITYDIS_TASK_DATE, upgradeDate.toString());
                redisCache.put(CommonConstant.UPGRADE_CITYDIS_TASK_TIME, taskTime.toString());
                resultMap.put("升级城主执行日期_新:", upgradeDate);
                resultMap.put("升级城主执行时间_新:", taskTime);
            }
            resultMap.put("升级城主执行日期_原:", upgrade_citydis_task_date);
            resultMap.put("升级城主执行时间_原:", upgrade_citydis_task_time);

        } else {
            String citydis_comiss_task_time = redisCache.getString(CommonConstant.CITYDIS_COMISS_TASK_TIME);
            if (onlyQuery == 1) {
                redisCache.put(CommonConstant.CITYDIS_COMISS_TASK_TIME, taskTime.toString());
                resultMap.put("每天-城主收益发放时间_新:", taskTime);
            }
            resultMap.put("每天-城主收益发放时间_原:", citydis_comiss_task_time);
        }
        return resultMap;
    }
}
